package com.cqjtu.eecs.exception;

import com.cqjtu.eecs.utils.Result;
import com.cqjtu.eecs.utils.ResultCode;
import io.jsonwebtoken.ExpiredJwtException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;


/**
 * @author luojun
 * @version 1.0
 * @date 2022/2/28 21:12
 * 系统全局异常处理
 */
@Slf4j
@RestControllerAdvice
public class SysExceptionHandler {

    //  插入教师信息时学院不存在异常处理
    @ExceptionHandler(value = AcademyNotFoundException.class)
    public Result academyNotFoundExceptionHandler(AcademyNotFoundException e) {
        return Result.error(-1, e.getMessage());
    }

    @ExceptionHandler(value = ExpiredJwtException.class)
    public Result ExpiredJwtExceptionHandler(ExpiredJwtException e) {
        return Result.error(-1, "Token expired");
    }

    @ExceptionHandler(value = InsertTeacherErrorException.class)
    public Result insertUserErrorExceptionHandler(InsertTeacherErrorException e) {
        return Result.error(-1, e.getMessage());
    }

    @ExceptionHandler(value = TeacherNotExistException.class)
    public Result teacherNotExistExceptionHandler(TeacherNotExistException e) {
        return Result.error(-1, e.getMessage());
    }

    @ExceptionHandler(value = CourseNotExistException.class)
    public Result courseNotExistExceptionHandler(CourseNotExistException e) {
        return Result.error(-1, e.getMessage());
    }

    @ExceptionHandler(value =Exception.class)
    public Result exceptionHandler(Exception e){
        log.error(e.getMessage());
        return Result.error(ResultCode.SYSTEM_ERROR);
    }
    /**
     * 对象类参数异常处理
     */
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(value = MethodArgumentNotValidException.class)
    public Result ParamsErrorExceptionHandler(MethodArgumentNotValidException e){
        log.error(e.getMessage());
        return Result.error(101,e.getMessage());
    }
    /**
     * 路径类参数异常处理
     */
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(value = ParamsErrorException.class)
    public Result ParamsErrorExceptionHandler2(ParamsErrorException e){
        log.error(e.getMessage());
        return Result.error(101,e.getMessage());
    }
    /**
     * 处理未找到异常
     */
    @ExceptionHandler(value = NotFoundException.class)
    public Result NotFoundExceptionHandler(NotFoundException e){
        log.error(e.getMessage());
        return Result.error(103,e.getMessage());
    }
    /**
     * 处理主键重复异常
     */
//    @ResponseStatus(HttpStatus.BAD_REQUEST)
//    @ExceptionHandler(value = DataAccessException.class)
//    public Result SQLIntegrityConstraintViolationExceptionHandler(DataAccessException e){
//        log.error(e.getMessage());
//        return Result.error(ResultCode.ALREADY_EXIST);
//    }
}
